package cn.edu.cqytxy;

import cn.edu.cqytxy.entity.Cat;

import java.io.*;
import java.util.*;

/**
 * @author yinjw
 */
public class CollectionTest {

    public static void main(String[] args) throws Exception {

        // url:xxxx
        // username: root
        // password: 1234

        Properties properties = new Properties();
        InputStream is = new FileInputStream("D:\\Temp\\file_test\\db.properties");
        properties.load(is);
        System.out.println(properties.getProperty("username"));
    }

    private static void test7() throws IOException{
        // 保存配置
        Properties properties = new Properties();
        properties.setProperty("username", "root");
        properties.setProperty("password", "1234");

        Writer writer = new FileWriter("D:\\Temp\\file_test\\db.properties");
        properties.store(writer, "db config");
    }

    private static void test6() {
        // K：key(键)，V：vlaue(值)
        Map<Integer, Cat> map = new HashMap<>();

        Cat cat1 = new Cat();
        cat1.setId(1);

        Cat cat2 = new Cat();
        cat2.setId(2);
        // put方法添加键值对数据,key值不能重复,重复会覆盖原有数据
        map.put(1, cat1);
        map.put(2, cat2);

        // 遍历
        Set<Map.Entry<Integer, Cat>> entrySet = map.entrySet();
        for (Map.Entry<Integer, Cat> entry : entrySet) {
            System.out.println("key=" + entry.getKey() + ",value="+entry.getValue());
        }

        // 获取一个数据
        System.out.println(map.get(1));

        // 通过所有的key值遍历数据
        Set<Integer> integers = map.keySet();
        for (Integer integer : integers) {
            System.out.println("key=" + integer + ", value="+map.get(integer));
        }

        // 通过迭代器遍历
        Iterator<Map.Entry<Integer, Cat>> iterator = map.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<Integer, Cat> next = iterator.next();
            System.out.println("key=" + next.getKey() + ",value="+next.getValue());
        }

        // forEach遍历
        map.forEach((key, value) -> System.out.println("key=" + key + ",value="+value));

        System.out.println("=================================");

        Collection<Cat> values = map.values();
        for (Cat value : values) {
            System.out.println(value);
        }
    }

    private static void test5() {
        Set<Cat> set = new HashSet<>();
        set.add(new Cat("小白", 3, "白色"));
        set.add(new Cat());

        Set<Cat> set2 = new HashSet<>();
        set.addAll(set2);

        for (Object o : set) {
            System.out.println(o);
        }
    }

    private static void test4() {
//        Collection
//        List
//        ArrayList

        List<Cat> list = new ArrayList<>();
        Cat item = new Cat();
        list.add(new Cat("小白", 3, "白色"));
        list.add(item);
        list.add(item);

        // 1 for循环遍历
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }
        for (Cat cat : list) {
            System.out.println(cat);
        }
//        list.forEach

        // 2 迭代器遍历
        Iterator<Cat> iterator = list.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
    }

    private static void test3() {
        // 尾插法
        List<Integer> list = new ArrayList<>();
        long start = System.currentTimeMillis();
        for (int i = 0; i < 10000000; i++) {
            list.add(i);
        }
        long end = System.currentTimeMillis();
        System.out.println(end - start);
    }

    private static void test2() {
        // 链表
        List<Cat> list = new LinkedList<>();
        list.add(new Cat());
        for (Cat cat : list) {
            System.out.println(cat);
        }
    }

    private static void test1() {
        // 申明一个数组
        int[] array = new int[10];

        // 申明一个集合List
        List<Cat> list = new ArrayList<>();

        list.add(new Cat());
        list.add(null);

        for (Cat cat : list) {
            if (cat == null) {
                continue;
            }
            System.out.println(cat.getAge());
        }
    }
}
